#!/bin/bash

# The substitution flag is set after the substitution command strings:
#   s/pattern/replacement/flags
# There are four types of substitution flags available:
# - A number: indicating the pattern occurrence for which new text should be
#   substituted.
# - g: Indicates that new text should be substituted for all occurrences of the
#   existing text.
# - p: Indicates that the contents of the original line should be printed.
# - w file: Write the results of the substitution to a file.
echo "This is a test" | sed 's/test/big test/'

seddata=seddata.txt
echo -e "The quick brown fox jumps over the lazy dog\n"\
"The quick green fox jumps over the lazy dog\n"\
"The quick brown fox jumps over the lazy dog\n"\
"The quick brown fox jumps over the lazy dog" > $seddata

# numeric addressing
echo "sed '2s/dog/cat/'"
sed '2s/dog/cat/' $seddata
echo "sed '2,3s/dog/cat/' $seddata"
sed '2,3s/dog/cat/' $seddata
echo "sed '2,\$s/dog/cat/' $seddata"
sed '2,$s/dog/cat/' $seddata
# pattern addressing
echo "sed '/green/s/dog/cat/' $seddata"
sed '/green/s/dog/cat/' $seddata
# group commands
echo "grouping commands"
sed '3,${
    s/fox/elephant/
    s/dog/cat/
}' $seddata

echo -e "This is line number 1\n"\
"This is line number 2\n"\
"This is line number 3\n"\
"This is line number 4" > $seddata

# delete lines
echo "sed '3d' $seddata"
sed '3d' $seddata
echo "sed '2,3d' $seddata"
sed '2,3d' $seddata
echo "sed '3,\$d' $seddata"
sed '3,$d' $seddata
# pattern
echo "sed '/1/,/3/d' $seddata"
sed '/1/,/3/d' $seddata

# insert/append/change
echo "insert line"
sed '3i\This is an inserted line.' $seddata
echo "append line"
sed '3a\This is an appended line.' $seddata
echo "change line"
sed '3c\This is a change line of text.' $seddata

# transform
echo "transfrom"
sed 'y/123/789/' $seddata

# write
echo "write"
sed -n '1,2w foo' $seddata
cat foo
rm foo

# read
echo "read"
seddata2=seddata2.txt
echo -e "This is an added line\n"\
"This is another added line"> $seddata2
sed "3r $seddata2" $seddata  # double quote must be used here?!
rm $seddata2
rm $seddata

